package farb.top.eurekaclientorderapp.controller;

import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class HelloController {
    @LoadBalanced
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("hello")
    public String hello() {
        // 一个服务可能在多个服务器节点上运行，所以一个服务可能对应多个实列
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("RiskService");
        if (!serviceInstances.isEmpty()) {
            return restTemplate.getForObject("http://RiskService/hello", String.class);
        }
        return "RiskService not found";
    }

    @GetMapping("getRiskInfo")
    public String getRiskInfo() {
        // 一个服务可能在多个服务器节点上运行，所以一个服务可能对应多个实列
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("RiskService");
        if (!serviceInstances.isEmpty()) {
            return restTemplate.getForObject("http://RiskService/getRiskLevel/003", String.class);
        }
        return "RiskService not found";
    }
}
